//
//  PersonTestCase.m
//  XCTestDemo
//
//  Created by zero on 2017/6/5.
//  Copyright © 2017年 zero. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "Person.h"

@interface PersonTestCase : XCTestCase

@end

@implementation PersonTestCase

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

//逻辑测试
- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
    [self checkPersonWithDict:@{@"name":@"abc",@"age":@"20"}];
    [self checkPersonWithDict:@{}];
    [self checkPersonWithDict:@{@"name":@"abc",@"age":@"-20"}];
}


- (void)checkPersonWithDict:(NSDictionary*)dict{
    Person* person = [Person personWithDictionary:dict];
    NSLog(@"dict:%@",dict);
    NSLog(@"person:%@",person);
    NSString* name = dict[@"name"];
    NSInteger age = [dict[@"age"]integerValue];
    
    XCTAssert([name isEqualToString:person.name] || person.name == nil, @"姓名不一致");
//    XCTAssert(person.age == age, @"年龄不一致");
    XCTAssert(person.age > 0 && person.age < 130 , @"年龄超限");
}


// 异步测试
- (void)testLoadPersonAsync {
    
    XCTestExpectation *expectation = [self expectationWithDescription:@"异步加载 Person"];
    
    [Person loadPersonAsync:^(Person *person) {
        //        NSLog(@"%@",person);
        
        XCTAssertNotNil(person.name, @"名字不能为空");
        XCTAssert(person.age > 0, @"年龄不正确");
        
        // 标注预期达成
        [expectation fulfill];
    }];
    
    // 等待 5s 期望预期达成
    [self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
        NSLog(@"超时");
    }];
}

//性能测试 - 每一个测试用例每次会运行10次
- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
        NSTimeInterval start = CACurrentMediaTime();
        
        for(NSInteger i=0; i<10000; i++){
            [Person personWithDictionary:@{@"name":@"abc",@"age":@"20"}];
        }
        
        NSLog(@"\n\ntime cost:%lf\n",CACurrentMediaTime() - start);
    }];
}

@end
